package cn.com.dhcc.app.pub.core.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.com.dhcc.app.core.annotation.SiNoNeedLogin;
import cn.com.dhcc.app.core.base.InterceptorBean;
import cn.com.dhcc.app.core.interceptor.InterceptorArgs;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;

/**
 * 拦截节点注册信息
 * @日期：2012-12-14下午11:21:12
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
@Component
public class NodeRegistInterceptor extends InterceptorBean {

	@Autowired
	BaseInfoService baseInfoService;
	public static final String INIT_NODE = "/registry/node_info_init.do";
	public static final String CHECK_NAME = "/registry/is_nodename_unique.do";
	public static final String ADD_INIT_NODE = "/registry/init_node_info.do";

	@Override
	public SIGNAL beforeControllerMethodExecute(InterceptorArgs interceptorArgs) {
		HandlerMethod handlerMethod = interceptorArgs.getHandlerMethod();
		HttpServletRequest request = interceptorArgs.getRequest();
		HttpServletResponse response = interceptorArgs.getResponse();
		if (request == null || request.getMethod() == null) {
			return SIGNAL.CONTINUE;
		}
		//无需登录权限认证
		try {
			
			
			if (null != handlerMethod.getMethodAnnotation(SiNoNeedLogin.class)||request.getRequestURI().toLowerCase().indexOf(INIT_NODE)!=-1 
					||request.getRequestURI().toLowerCase().indexOf(CHECK_NAME)!=-1 
					||request.getRequestURI().toLowerCase().indexOf(ADD_INIT_NODE)!=-1) {
				return SIGNAL.CONTINUE;
			} else {
				Object user = getSessionVo(interceptorArgs);
				if(user!=null){
					try {
						NodeInfo nodeInfo = baseInfoService.getCurrentNodeInfo();
						if(nodeInfo==null){
							response.sendRedirect(request.getContextPath()+INIT_NODE);
						}else{
							String orgCode = NodeInfoParam.NODE_REGION_CODE;
							if(nodeInfo.getNodeCode().indexOf(orgCode+ NodeInfoParam.NODE_NET_TYPE) == -1){
								response.sendRedirect(request.getContextPath()+INIT_NODE);
							}
						}
						
					} catch (Exception e) {
						try {
							response.sendRedirect(request.getContextPath()+INIT_NODE);
						} catch (IOException e1) {
						}
					}
				}
			}
		} catch (Exception e) {
			return SIGNAL.CONTINUE;
		}
		return SIGNAL.CONTINUE;
	}

	@Override
	public void afterControllerMethodExecute(InterceptorArgs interceptorArgs, ModelAndView modelAndView) {
	}

	@Override
	public SIGNAL interceptorExecuteErrorSignal(Exception beforeControllerMethodExecuteException) {
		return SIGNAL.CONTINUE;
	}
	private Object getSessionVo(InterceptorArgs interceptorArgs) {
		Object sessionVo = interceptorArgs.getUserInfo();
		return sessionVo;
	}
}
